package springboot.jdbc;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.*;
import java.util.Objects;

public class HikariUtils {

    private static HikariDataSource ds ;

    static {
        String configFile = Objects.requireNonNull(HikariUtils.class.getClassLoader().getResource("db.properties")).getPath();

        HikariConfig cfg = new HikariConfig(configFile);
        ds = new HikariDataSource(cfg);
    }

    //获得连接
    public static Connection getConnections() throws SQLException {
        Connection conn = ds.getConnection();
        return conn;
    }

    //释放资源
    public static void closeResource(Connection conn, Statement st, ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {

            };
        }

        if(st != null) {
            try {
                st.close();
            } catch (SQLException e) {
            }
        }

        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
}
